#include "MPU9250.h"

long now;
long pre_time;

unsigned long count=0;
bool filen=false;

IMU_EN_SENSOR_TYPE enMotionSensorType;
extern Mpu_Data mpu_data_s;

char mkdir_txt(void)
{
	char filename[30];
	char str1[]="bolangyi_";
	struct tm *newtime; 
	time_t long_time;
	time(&long_time); 
	newtime=localtime(&long_time);
	sprintf(filename,"%d_%d_%d_%d",newtime->tm_year+1900,newtime->tm_mon+1,newtime->tm_mday,newtime->tm_hour,newtime->tm_min,newtime->tm_sec);
	strcat(str1, filename);
	strcat(str1, ".txt");
	
	FILE *pFile;
	pFile = fopen(str1, "a");//这个用“w”是写文件，覆盖原内容，若不想覆盖则用“a”
	if (NULL == pFile)
	{
		perror("error");
		return false;
	}
	fclose(pFile);//最后一定记得关闭文件
	return str1;
	
}

bool txtsave(char *str, Mpu_Data *pst)
{
	char filename[30]="bolangyi.txt";
	FILE *pFile;
	pFile = fopen(filename, "a");//这个用“w”是写文件，覆盖原内容，若不想覆盖则用“a”
	if (NULL == pFile)
	{
		perror("error");
		return false;
	}

	fprintf(pFile,"Roll: %.2f ,Pitch: %.2f ,Yaw: %.2f ,",pst->Roll, pst->Pitch, pst->Yaw);
	fprintf(pFile,"  Acceleration X: %d ,Y: %d ,Z: %d ,",pst->ACC.x, pst->ACC.y, pst->ACC.z);
	fprintf(pFile,"  Gyroscope X: %d ,Y: %d ,Z: %d ,",pst->GYRO.x, pst->GYRO.y, pst->GYRO.z);
	fprintf(pFile,"  Magnetic X: %d ,Y: %d ,Z: %d ,",pst->MAG.x, pst->MAG.y, pst->MAG.z);
	fprintf(pFile,"\r\n");
	fclose(pFile);//最后一定记得关闭文件
	return true;
}

void myInterrupt (void) 
{
	bool FLAG=false;
 	now=micros();
 	printf ("now: %ld ,now-pre_time: %ld\n",now,now-pre_time);
 	pre_time=now;

 	imuDataGet(&mpu_data_s);
 	printf("\r\n /-------------------------------------------------------------/ \r\n");
 	printf("\r\n Roll: %.2f     Pitch: %.2f     Yaw: %.2f \r\n",mpu_data_s.Roll, mpu_data_s.Pitch, mpu_data_s.Yaw);
 	printf("\r\n Acceleration: X: %d     Y: %d     Z: %d \r\n",mpu_data_s.ACC.x, mpu_data_s.ACC.y, mpu_data_s.ACC.z);
 	printf("\r\n Gyroscope: X: %d     Y: %d     Z: %d \r\n",mpu_data_s.GYRO.x, mpu_data_s.GYRO.y, mpu_data_s.GYRO.z);
 	printf("\r\n Magnetic: X: %d     Y: %d     Z: %d \r\n",mpu_data_s.MAG.x, mpu_data_s.MAG.y, mpu_data_s.MAG.z);
 	FLAG=txtsave(0,&mpu_data_s);
 	count++;
 	printf("FLAG: %d , count: %ld \r\n",FLAG,count);

}

int main(int argc, char* argv[])
{
	imuInit(&enMotionSensorType);
	if(IMU_EN_SENSOR_TYPE_MPU9250 == enMotionSensorType)
	{
		printf("Motion sersor is MPU-9250\n" );
	}
	else
	{
		printf("Motion sersor NULL\n");
	}
	pinMode(22, INPUT);
    wiringPiISR (22, INT_EDGE_FALLING, &myInterrupt);
	while(1)
	{
/*
		delay(2000);
		imuDataGet(&mpu_data_s);
 		printf("\r\n /-------------------------------------------------------------/ \r\n");
 		printf("\r\n Roll: %.2f     Pitch: %.2f     Yaw: %.2f \r\n",mpu_data_s.Roll, mpu_data_s.Pitch, mpu_data_s.Yaw);
 		printf("\r\n Acceleration: X: %d     Y: %d     Z: %d \r\n",mpu_data_s.ACC.x, mpu_data_s.ACC.y, mpu_data_s.ACC.z);
 		printf("\r\n Gyroscope: X: %d     Y: %d     Z: %d \r\n",mpu_data_s.GYRO.x, mpu_data_s.GYRO.y, mpu_data_s.GYRO.z);
 		printf("\r\n Magnetic: X: %d     Y: %d     Z: %d \r\n",mpu_data_s.MAG.x, mpu_data_s.MAG.y, mpu_data_s.MAG.z);
*/
	}
	return 0;
}
